﻿#define  _CRT_SECURE_NO_WARNINGS 1
#include "snake.h"
void SetPos(int x, int y)
{
	//获取句柄
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	//根据句柄设置光标的位置
	COORD pos = { x,y };
	SetConsoleCursorPosition(handle,pos);
}
void WelcomeToGame()
{
	//打印的是欢迎信息
	SetPos(40, 13);
	printf("欢迎来到贪吃蛇游戏");
	SetPos(40, 25);
	system("pause");
	system("cls");
	
	//打印功能信息
	SetPos(30,13);
	printf("用↑.↓.←.→ 分别控制蛇的移动， F3为加速，F4为减速\n");
	SetPos(40,14);
	printf("加速能得到更高的分数");
	SetPos(40, 25);
	system("pause");
	system("cls");
}
void CreateMap()
{
	//上
	SetPos(0, 0);
	int i = 0;
	for (i = 0; i <= 56; i += 2)
	{
		wprintf(L"%lc", WALL);
	}
	//下
	SetPos(0, 25);
	for (i = 0; i <= 56; i += 2)
	{
		wprintf(L"%lc",WALL);
	}
	//左
	for (i = 0; i <= 25; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	//右
	for (i = 0; i <= 25; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}

}
void InitSnake(pSnake ps) 
{
	//创建5个蛇身的节点
	pSnakeNode cur = NULL;
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake():malloc()");
			return;
		}
		cur->x = POS_X + 2 * i;
		cur->y = POS_Y;
		cur->next = NULL;
		//头插
		if (ps->pSnake == NULL)
			ps->pSnake = cur;
		else
		{
			cur->next = ps->pSnake;
			ps->pSnake = cur;
		}

	}
	//打印蛇身
	cur = ps->pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//蛇的其他初始化
	ps->dir = RIGHT;
	ps->Foodweight = 10;
	ps->pFood = NULL;
	ps->Score = 0;
	ps->SleepTime = 200;
	ps->status = OK;

	
}
void CreateFood(pSnake ps)
{
	int x = 0;
	int y = 0;
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 24 + 1;
	} while (x % 2 != 0);
	//食物坐标和蛇的每个节点坐标比较
	pSnakeNode cur = ps->pSnake;
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			do
			{
				x = rand() % 53 + 2;
				y = rand() % 24 + 1;
			} while (x % 2 != 0);
		}
		cur = cur->next;
	}
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(pSnakeNode));
	if (pFood == NULL)
	{
		perror("CreateFood():malloc()");
		return;
	}
	pFood->x = x;
	pFood->y = y;
	ps->pFood = pFood;
	SetPos(x, y);
	wprintf(L"%lc", FOOD);

}

void GameStart(pSnake ps)
{
	//设置控制台的信息，窗口的大小，窗口名
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	//隐藏光标
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(handle, &CursorInfo);
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(handle, &CursorInfo);

	//打印欢迎信息
	WelcomeToGame();
	//绘制地图
	CreateMap();

	//初始化蛇
	InitSnake(ps);
	//创建食物
	CreateFood(ps);
}
void PrintHelpInfo()
{
	SetPos(65,15);
	printf("1.不能穿墙，不能咬到自己");
	SetPos(65,16);
	printf("2.用↑.↓.←.→ 分别控制蛇的移动");
	SetPos(65, 17);
	printf("3.F3是加速，F4是减速");
}

void pause()
{
	while (1)
	{
		Sleep(100);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}
int NextIsFood(pSnake ps, pSnakeNode pNext)
{
	if (ps->pFood->x == pNext->x && ps->pFood->y == pNext->y)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}


void EatFood(pSnake ps, pSnakeNode pNext)
{
	pNext->next = ps->pSnake;
	ps->pSnake = pNext;
	//打印蛇
	pSnakeNode cur = ps->pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	ps->Score += ps->Foodweight;
	free(ps->pFood);
	CreateFood(ps);
}

void NotEatFood(pSnake ps, pSnakeNode pNext)
{
	//头插
	pNext->next = ps->pSnake;
	ps->pSnake = pNext;

	//尾节点释放
	pSnakeNode cur = ps->pSnake;
	while (cur->next->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//将尾节点的位置打印成空白字符
	SetPos(cur->next->x, cur->next->y);
	printf("  ");

	free(cur->next);
	cur->next = NULL;


}

void Killbywall(pSnake ps)
{
	if (ps->pSnake->x == 0 ||
		ps->pSnake->x == 56 ||
		ps->pSnake->y == 0 ||
		ps->pSnake->y == 25)
	{
		ps->status = KILL_BY_WALL;
	}
}

void Killbyself(pSnake ps)
{
	pSnakeNode cur = ps->pSnake->next;//从第二个节点开始
	while (cur)
	{
		if (cur->x == ps->pSnake->x && cur->y == ps->pSnake->y)
		{
			ps->status = KILL_BY_SELF;
			return;
		}
		cur = cur->next;
	}

}

void SnakeMove(pSnake ps)
{
	pSnakeNode pNext=(pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNext == NULL)
	{
		perror("SnakeMove():malloc()");
		return;
	}
	pNext->next = NULL;
	switch (ps->dir)
	{
	case UP:
		pNext->x = ps->pSnake->x;
		pNext->y = ps->pSnake->y - 1;
		break;
	case DOWN:
		pNext->x = ps->pSnake->x;
		pNext->y = ps->pSnake->y + 1;
		break;
	case LEFT:
		pNext->x = ps->pSnake->x-2;
		pNext->y = ps->pSnake->y ;
		break;
	case RIGHT:
		pNext->x = ps->pSnake->x + 2;
		pNext->y = ps->pSnake->y;
		break;
	}
	//下一个坐标是不是食物
	if (NextIsFood(ps, pNext))
	{
		//是食物
		EatFood(ps,pNext);
	}
	else
	{
		//不是食物
		NotEatFood(ps, pNext);
	}
	
	//检测撞墙
	Killbywall(ps);

	//检测撞到自己
	Killbyself(ps);
}


void GameRun(pSnake ps)
{
	//打印帮助信息
	PrintHelpInfo();
	do
	{
		//当前分数情况
		SetPos(65, 10);
		printf("总分:%d\n", ps->Score);
		SetPos(65, 11);
		printf("食物的分值:%d\n", ps->Foodweight);
		//检测按键
        //上 下 左 右 F3 F4 ESC 空格
		if (KEY_PRESS(VK_UP) && ps->dir != DOWN)
		{
			ps->dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->dir != UP)
		{
			ps->dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->dir != RIGHT)
		{
			ps->dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->dir != LEFT)
		{
			ps->dir = RIGHT;
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->status = ESC;
			break;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			pause();
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->SleepTime >= 80)
			{
				ps->SleepTime -= 30;
				ps->Foodweight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->Foodweight > 2)
			{
				ps->SleepTime += 30;
				ps->Foodweight -= 2;
			}
		}
		//睡眠一下
		Sleep(ps->SleepTime);
		//走一步
		SnakeMove(ps);

	} while (ps->status == OK);

}

//游戏的结束
void GameEnd(pSnake ps)
{
	SetPos(15, 12);
	switch (ps->status)
	{
	case ESC:
		printf("主动退出游戏,正常退出\n");
		break;
	case KILL_BY_WALL:
		printf("很遗憾，撞墙了，游戏结束\n");
		break;
	case KILL_BY_SELF:
		printf("很遗憾，撞到自己了，游戏结束\n");
		break;
	}
	//释放贪吃蛇链表
	pSnakeNode cur = ps->pSnake;
	pSnakeNode del = NULL;
	while (cur)
	{
		del = cur;
		cur = cur->next;
		free(del);
	}
	
	free(ps->pFood);
	ps = NULL;
}